package com.guang.testspringmybatis.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.io.IOException;

/**
 * @author liguang
 * @date 2022/8/24 11:47
 */
@Configuration
@ComponentScan("com.guang.testspringmybatis")
public class AppConfig {


    private final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();


    /**
     * SpringBoot中，这里也不应该来写死这里的规则
     *
     * 硬编码问题应该写在配置文件中来
     * @return
     * @throws PropertyVetoException
     */
    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ie_review");
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        return dataSource;
    }

    /**
     * 在springboot中可以来从外部进行配置
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        // 想自定义来设置一些属性配置信息
        // 如果可以指定配置文件来进行配置其中的一些无关的属性就非常好了
        // ClassPathResource classPathResource = new ClassPathResource("classpath*:mybatis-config.xml");
        // sqlSessionFactoryBean.setConfigLocation(classPathResource);
        sqlSessionFactoryBean.setConfigLocation(getConfigLocation());
        sqlSessionFactoryBean.setMapperLocations(getMapperLocations());
        sqlSessionFactoryBean.setTypeAliasesPackage("com.guang.testspringmybatis.pojo");
        // 插件等其他的可以在外部来进行设置和维护
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.guang.testspringmybatis.mapper");
        return mapperScannerConfigurer;
    }


    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager() throws PropertyVetoException {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource());
        return dataSourceTransactionManager;
    }


    private Resource getConfigLocation(){
        return this.resourceResolver.getResource("classpath:mybatis/mybatis-config.xml");
    }


    private Resource[] getMapperLocations() throws IOException {
        Resource[] mapperLocations = this.resourceResolver.getResources("classpath:com/guang/testspringmybatis/mapper/*Mapper.xml");
/*        for(int var5 = 0; var5 < var4; ++var5) {
            String mapperLocation = var3[var5];
            Resource[] addedMapperLocations = this.resourceResolver.getResources(mapperLocation);
            Resource[] originMapperLocations = mapperLocations;
            mapperLocations = new Resource[mapperLocations.length + addedMapperLocations.length];
            System.arraycopy(originMapperLocations, 0, mapperLocations, 0, originMapperLocations.length);
            System.arraycopy(addedMapperLocations, 0, mapperLocations, originMapperLocations.length, addedMapperLocations.length);
        }*/

        return mapperLocations;
    }

}
